/**
 *  *2010-6-13 下午04:13:35  add 
 */
package com.base.myproject.client.ui.test;

import java.util.ArrayList;
import java.util.List;

import org.swfupload.client.File;
import org.swfupload.client.SWFUpload;
import org.swfupload.client.UploadBuilder;
import org.swfupload.client.SWFUpload.ButtonAction;
import org.swfupload.client.event.DialogStartHandler;
import org.swfupload.client.event.FileDialogCompleteHandler;
import org.swfupload.client.event.FileQueueErrorHandler;
import org.swfupload.client.event.FileQueuedHandler;
import org.swfupload.client.event.SWFUploadLoadedHandler;
import org.swfupload.client.event.UploadCompleteHandler;
import org.swfupload.client.event.UploadErrorHandler;
import org.swfupload.client.event.UploadProgressHandler;
import org.swfupload.client.event.UploadStartHandler;
import org.swfupload.client.event.UploadSuccessHandler;

import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Administrator 2010-6-13 javazgw@gmail.com
 */
public class SWFUploadtext extends LayoutContainer {
	private List<File> files = new ArrayList<File>();
	int filecount = 0;
	final HTML html = new HTML("");
	SWFUpload upload;
	UploadBuilder builder = new UploadBuilder();
	public SWFUploadtext() {
		//init();
		super();
		//  upload = builder.build();
		
		
	}

public void aa (){
	
}
//onRender

//<<<<<<< .mine
	protected  void onRender(Element target, int index) {
	//	super.onRender(target, index);
		//super.onRender(target, index);  
		VerticalPanel hp = new VerticalPanel();
		hp.setSpacing(5);
		add(hp);
		// hp.setWidth("100%");


		
		builder
				.setFileTypes("*.asf;*.wma;*.wmv;*.avi;*.flv;*.swf;*.mpg;*.mpeg;*.mp4;*.mov;*.m4v;*.aac;*.mp3;*.wav;*.png;*.jpg;*.jpeg;*.gif");
		builder.setFileTypesDescription("Images, Video & Sound");

		// Configure the button to display
		builder.setButtonPlaceholderID("swfupload");
		builder.setButtonImageURL("images/baseimage/swfupload.png");
		builder.setButtonWidth(200);
		builder.setButtonHeight(18);
	//	builder.setButtonText("<span class=\"label\">请选择文件</span>");
		builder.setButtonText("<span class='button'>请选择文件 <span class='buttonSmall'>(最大 2 MB )</span></span>");
		
		builder.setButtonTextStyle(".button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }");
//		builder
//				.setButtonTextStyle(".label { color: #011000; font-family: sans font-size: 16pt;width=200px }");
	       
//<<<<<<< .mine
		
		builder.setButtonTextLeftPadding(18);
		builder.setButtonTextTopPadding(0);
		builder.setFileQueueLimit(10);
		builder.setFileUploadLimit(20);
		builder.setFileSizeLimit(2048);
		builder.setDebug(false);
//=======
	       // The button to start the transfer
	       Button uploadButton = new Button("Upload");
	       //position:absolute;
	       uploadButton.addStyleName("upload_button");
	    //   RootPanel.get().add(uploadButton);
//>>>>>>> .r122

	//	builder.setUploadURL("gwtbase/BaseFileUploadServletFinal");

		// ---------------

		// ---------------
		builder.setUploadStartHandler(new UploadStartHandler() {

			@Override
			public void onUploadStart(UploadStartEvent e) {
				GWT.log("UploadStartHandler");

			}

		});
		builder.setUploadProgressHandler(new UploadProgressHandler() {

			@Override
			public void onUploadProgress(UploadProgressEvent e) {
				GWT
						.log("UploadProgressHandler complete="
								+ e.getBytesComplete() + " total ="
								+ e.getBytesTotal());
				File f = e.getFile();
				f.getName();
				// String text = html.getHTML();
				// text += "<br />" + e.getBytesComplete() + "; " + f.getName();
				// html.setHTML(text);

			}

		});
		builder.setUploadErrorHandler(new UploadErrorHandler() {

			@Override
			public void onUploadError(UploadErrorEvent e) {
				File ff = e.getFile();
				// String message = e.getMessage();
				// if (message == null || message.trim().length() == 0) {
				// message = "upload failed";
				// }
				// String t = html.getHTML();
				// t += "<br />error: " + ff.getId() + ", " + ff.getName() +
				// " / " + message;
				// html.setHTML(t);
				removeFile(ff.getId());
				if (files.size() > 0) {
					ff = files.get(0);
					String id = ff.getId();
					// String tt = html.getHTML();
					// tt += "<br />start: " + id;
					// html.setHTML(tt);
					upload.startUpload(id);
				}
			}

		});

		builder.setSWFUploadLoadedHandler(new SWFUploadLoadedHandler() {

			@Override
			public void onSWFUploadLoaded() {
				GWT.log("SWFUploadLoadedHandler");

			}

		});
		builder.setFileQueueErrorHandler(new FileQueueErrorHandler() {

			@Override
			public void onFileQueueError(FileQueueErrorEvent e) {
				GWT.log("FileQueueErrorHandler");

			}

		});

		builder.setFileQueuedHandler(new FileQueuedHandler() {

			@Override
			public void onFileQueued(FileQueuedEvent event) {

				// String t = html2.getHTML();
				// t += "<br />ofq: " + event.getFile().getId() + "; "
				// + event.getFile().getName();
				// html2.setHTML(t);
				files.add(event.getFile());
			}

		});
		builder.setFileDialogCompleteHandler(new FileDialogCompleteHandler() {

			@Override
			public void onFileDialogComplete(FileDialogCompleteEvent e) {
				html.setHTML("你选择了 = " + files.size() + "个文件");

			}

		});
		builder.setDialogStartHandler(new DialogStartHandler() {

			@Override
			public void onDialogStart() {
				// File ff = files.get(0);
				// String id = ff.getId();
				// String t = html.getHTML();
				// t += "<br />start: " + id;
				// html.setHTML(t);
				// upload.startUpload(id);

			}

		});
		// Use ButtonAction.SELECT_FILE to only allow selection of a single file
		builder.setButtonAction(ButtonAction.SELECT_FILES);

		// The placeholder to be replaced by the swfupload control
		// This could also be provided statically by your host HTML page
		 Element placeHolder = DOM.createDiv();
		 placeHolder.setId("swfupload");
		 //RootPanel.get().getElement().appendChild(placeHolder);

		// The button to start the transfer
		 
		 Html h = new Html("<div style=\"width: 180px; height: 18px; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;\"><span id='swfupload'></span> <div>");
		//Button uploadButton = new Button("Upload");
		
		HorizontalPanel hpanel = new HorizontalPanel();
		
		//hpanel.getElement().appendChild(placeHolder);
		hpanel.add(h);
		hpanel.add(uploadButton);
		
		hp.add(hpanel);
		hp.add(html);

		builder.setUploadCompleteHandler(new UploadCompleteHandler() {

			@Override
			public void onUploadComplete(UploadCompleteEvent e) {
				GWT.log("UploadCompleteHandler");
				File f = e.getFile();
				String t = html.getHTML();
				t += "<br />上传完成 : " + f.getId() + ", " + f.getName();
				html.setHTML(t);
				GWT.log("files.size()=" + files.size());
				removeFile(f.getId());
				if (files.size() > 0) {
					File ff = files.get(0);
					String id = ff.getId();
					// String tt = html.getHTML();
					// tt += "<br />start: " + id;
					// html.setHTML(tt);
					// GWT.log("id="+id);
					upload.startUpload(id);
				}

			}

		});

		builder.setUploadSuccessHandler(new UploadSuccessHandler() {

			@Override
			public void onUploadSuccess(UploadSuccessEvent e) {
				GWT.log("UploadSuccessHandler");
				// String t = html.getHTML();
				// t += "<br />server data : " + e.getServerData();
				// html.setHTML(t);
			}

		});

		uploadButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				// while(filecount>0)
				{
					upload.startUpload();
					// filecount--;
				}
			}

		});
		
	}

	private void removeFile(String id) {
		for (File ff : files) {
			if (ff.getId().equals(id)) {
				files.remove(ff);
			}
		}
	}


	public SWFUpload getUpload() {
		return upload;
	}


	public void setUpload(SWFUpload upload) {
		this.upload = upload;
	}


	public UploadBuilder getBuilder() {
		return builder;
	}


	public void setBuilder(UploadBuilder builder) {
		this.builder = builder;
	}

}
